﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using BussinessLogicTecnoPez;
using BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques;
using BussinessLogicTecnoPez.Tipos_Comunes;
using Tecnopez.Globals;
using Microsoft.Practices.EnterpriseLibrary.Validation;

public partial class Modules_EnfermedadesControlEstanque_ABMVeterinarios : System.Web.UI.Page
{
    private GridView grdVeterinarios;
    protected void Page_Load(object sender, EventArgs e)
    {
        grdVeterinarios = TecnopezDataGRIDABMView1.grdDatosAbmView;
        if (IsPostBack == false)
        {
            SetMode(PageMode.ListMode);
        }

        InicializarValidadores();
        InicializarGrilla();

        cmdEliminar.Attributes.Add("onClick", "return confirm('Esta seguro de eliminar el registro?');");

        GenericMasterPage master = (GenericMasterPage)this.Master;
        master.Imagen = "../Styles/Icons/veterinarios.gif";
        master.Titulo = "Gestión de veterinarios";
        cargarGrilla();
        _lblError.Text = "";
    }

    private void InicializarValidadores()
    {
        PPVNombre.SourceTypeName = "BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques.Veterinario";
        PPVNombre.ControlToValidate = "txtNombre";
        PPVNombre.PropertyName = "Nombre";
        PPVNombre.DisplayMode = ValidationSummaryDisplayMode.List;

        PPVApellido.SourceTypeName = "BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques.Veterinario";
        PPVApellido.ControlToValidate = "txtApellido";
        PPVApellido.PropertyName = "Apellido";
        PPVApellido.DisplayMode = ValidationSummaryDisplayMode.List;
    }

    private void cargarGrilla()
    {
        Veterinario.getManager.ClearFilters();
        Veterinario.getManager.ClearOrders();
        Veterinario.getManager.AddFilter("NOMBRE", GenericManager<Veterinario>.filterOperator.Like, "%" + txtBuscarNombre.Text.Trim() + "%");
        Veterinario.getManager.AddFilter("APELLIDO", GenericManager<Veterinario>.filterOperator.Like, "%" + txtBuscarApellido.Text.Trim() + "%");

        if (txtBuscarMatricula.Text.Trim() != "")
        {
            Veterinario.getManager.AddFilter("Matricula", GenericManager<Veterinario>.filterOperator.Equals, txtBuscarMatricula.Text.Trim());
        }

        if (ViewState["orden"] == null)
        {
            Veterinario.getManager.AddOrder("IDVETERINARIO", GenericManager<Veterinario>.Order.orderDirection.DESC);
        }
        else
        {
            Veterinario.getManager.AddOrder(ViewState["orden"].ToString(), (GenericManager<Veterinario>.Order.orderDirection)ViewState["ordenDireccion"]);

        }

        List<Veterinario> lista = new List<Veterinario>();
        lista = Veterinario.getManager.GetFilter();
        grdVeterinarios.DataSource = lista;
        grdVeterinarios.DataBind();
    }

    private void seleccionarVeterinario(int o)
    {
        cargarGrilla();
        int IDVETERINARIO = int.Parse(grdVeterinarios.DataKeys[o].Value.ToString());
        Veterinario.getManager.ClearFilters();
        Veterinario.getManager.AddFilter("IDVETERINARIO", BussinessLogicTecnoPez.GenericManager<Veterinario>.filterOperator.Equals, IDVETERINARIO);
        Veterinario auxVeterinario = Veterinario.getManager.GetOne();
        txtID.Text = auxVeterinario.IdVeterinario.ToString();
        txtNombre.Text = auxVeterinario.Nombre.Trim();
        txtApellido.Text = auxVeterinario.Apellido.Trim();
        txtCorreoElectronico.Text = auxVeterinario.CorreoElectronico.Trim();
        txtDireccion.Text = auxVeterinario.Direccion.Trim();
        txtMatricula.Text = auxVeterinario.Matricula.Trim();
        txtTelefono.Text = auxVeterinario.Telefono.Trim();
        SetMode(PageMode.ViewMode);
    }

    private void SetMode(PageMode mode)
    {
        ViewState["PageMode"] = mode;
        switch (mode)
        {
            case PageMode.ListMode:
                grdVeterinarios.Enabled = true;
                cmdBuscar.Enabled = true;
                cmdNuevo.Enabled = true;
                wzrABM.MoveTo(wzrABM.WizardSteps[0]);
                break;
            case PageMode.NewMode:
                grdVeterinarios.Enabled = false;
                cmdBuscar.Enabled = false;
                cmdNuevo.Enabled = false;
                wzrABM.ActiveStepIndex = 1;
                txtID.Enabled = false;
                txtNombre.Enabled = true;
                txtApellido.Enabled = true;
                txtCorreoElectronico.Enabled = true;
                txtDireccion.Enabled = true;
                txtMatricula.Enabled = true;
                txtTelefono.Enabled = true;
                cmdAceptar.Visible = true;
                cmdCancelar.Enabled = true;
                cmdEditar.Enabled = false;
                cmdEliminar.Enabled = false;
                break;
            case PageMode.EditMode:
                grdVeterinarios.Enabled = false;
                cmdBuscar.Enabled = false;
                cmdNuevo.Enabled = false;
                wzrABM.MoveTo(wzrABM.WizardSteps[1]);
                txtID.Enabled = false;
                txtNombre.Enabled = true;
                txtApellido.Enabled = true;
                txtCorreoElectronico.Enabled = true;
                txtDireccion.Enabled = true;
                txtMatricula.Enabled = true;
                txtTelefono.Enabled = true;
                cmdAceptar.Visible = true;
                cmdCancelar.Enabled = true;
                cmdEditar.Enabled = false;
                cmdEliminar.Enabled = false;
                break;
            case PageMode.ViewMode:
                grdVeterinarios.Enabled = false;
                cmdBuscar.Enabled = false;
                cmdNuevo.Enabled = false;
                wzrABM.MoveTo(wzrABM.WizardSteps[1]);
                txtID.Enabled = false;
                txtNombre.Enabled = false;
                txtApellido.Enabled = false;
                txtCorreoElectronico.Enabled = false;
                txtDireccion.Enabled = false;
                txtMatricula.Enabled = false;
                txtTelefono.Enabled = false;
                txtID.ForeColor = System.Drawing.Color.Black;
                txtNombre.ForeColor = System.Drawing.Color.Black;
                txtApellido.ForeColor = System.Drawing.Color.Black;
                txtCorreoElectronico.ForeColor = System.Drawing.Color.Black;
                txtDireccion.ForeColor = System.Drawing.Color.Black;
                txtMatricula.ForeColor = System.Drawing.Color.Black;
                txtTelefono.ForeColor = System.Drawing.Color.Black;
                cmdAceptar.Visible = false;
                cmdCancelar.Enabled = true;
                cmdEditar.Enabled = true;
                cmdEliminar.Enabled = true;
                break;
        }

    }

    protected void cmdEditar_Click(object sender, EventArgs e)
    {
        SetMode(PageMode.EditMode);
        cargarGrilla();
    }

    protected void cmdEliminar_Click(object sender, EventArgs e)
    {
        //mensage confirmación
        Veterinario auxVeterinario = new Veterinario();
        auxVeterinario.IdVeterinario = int.Parse(txtID.Text.Trim());
        int res = Veterinario.getManager.Delete(auxVeterinario);
        cargarGrilla();
        SetMode(PageMode.ListMode);
    }

    protected void cmdNuevo_Click(object sender, EventArgs e)
    {
        txtID.Text = "";
        txtNombre.Text = "";
        txtApellido.Text = "";
        txtCorreoElectronico.Text = "";
        txtDireccion.Text = "";
        txtMatricula.Text = "";
        txtTelefono.Text = "";
        SetMode(PageMode.NewMode);
    }

    protected void cmdAceptar_Click(object sender, EventArgs e)
    {
        try
        {
            Veterinario auxVeterinario = new Veterinario();
            auxVeterinario.Nombre = txtNombre.Text.Trim();
            auxVeterinario.Apellido = txtApellido.Text.Trim();
            auxVeterinario.CorreoElectronico = txtCorreoElectronico.Text.Trim();
            auxVeterinario.Direccion = txtDireccion.Text.Trim();
            auxVeterinario.Matricula = txtMatricula.Text.Trim();
            auxVeterinario.Telefono = txtTelefono.Text.Trim();
            if (validar(auxVeterinario))
            {

                if (getMode() == PageMode.NewMode)
                {
                    if (Veterinario.getManager.Insert(auxVeterinario) != true)
                    {
                        throw Veterinario.getManager.GeneratedException;
                    }
                    SetMode(PageMode.ListMode);
                }
                else
                {
                    auxVeterinario.IdVeterinario = int.Parse(txtID.Text.ToUpper().Trim());
                    if (Veterinario.getManager.Update(auxVeterinario) != true)
                    {
                        throw Veterinario.getManager.GeneratedException;
                    }
                    SetMode(PageMode.ViewMode);
                }
            }
            cargarGrilla();
        }
        catch (Exception exc)
        {
            _lblError.Text = "error: No se pudo realizar la operación";
            Functions.LogError(exc);
            SetMode(PageMode.NewMode);
        }
    }

    protected void cmdCancelar_Click(object sender, EventArgs e)
    {
        SetMode(PageMode.ListMode);
    }

    private void InicializarGrilla()
    {
        DataControlField[] d = new DataControlField[1];

        ImageField colC = new ImageField();
        colC.DataImageUrlField = "IDVETERINARIO";
        colC.HeaderText = "Email";
        colC.HeaderStyle.Width = 5;
        colC.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        d[0] = colC;

        TecnopezDataGRIDABMView1.setupGrid<Veterinario>(cargarGrilla, seleccionarVeterinario, grdVeterinario_RowDataBound, false, d, ViewState);

    }

    private bool validar(Veterinario objeto)
    {
        ValidationResults v = Validation.Validate<Veterinario>(objeto);
        if (!v.IsValid)
        {
            _lblError.Text = "Error en el formulario: Por favor verifique los campos ingresados.";
        }
        return v.IsValid;

    }

    private PageMode getMode()
    {
        if (ViewState["PageMode"] == null)
            return PageMode.ModeNotFound;
        return (PageMode)ViewState["PageMode"];
    }

    protected void grdVeterinario_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int IndiceEmail = 0;

        for (int celda = 0; celda < grdVeterinarios.Columns.Count; celda++)
        {
            if (grdVeterinarios.Columns[celda].HeaderText == "Email")
            {
                IndiceEmail = celda;
            }
        }

        if (e.Row.DataItem != null)
        {
            if (e.Row.DataItem is Veterinario)
            {
                Image img = (Image)e.Row.Cells[IndiceEmail].Controls[0];
                Veterinario vet = (Veterinario)e.Row.DataItem;
                if (vet.CorreoElectronico.Trim() != "")
                {
                    img.ImageUrl = Functions.getHost(this.Page) + "Styles/Icons/email.gif";
                    img.ToolTip = vet.CorreoElectronico;
                    img.Style.Add("cursor", "hand");
                    img.Attributes.Add("onclick", "javascript:mail('" + vet.CorreoElectronico + "')");
                }
                else
                {
                    img.ImageUrl = Functions.getHost(this.Page) + "Styles/Icons/noemail.gif";
                    img.Attributes.Add("onclick", "return false");
                }
            }
        }
    }
    protected void cmdBuscar_Click(object sender, EventArgs e)
    {

    }
}
